package com.bc.utils;

import java.text.DecimalFormat;

import com.bc.R;
import com.bc.ui.BtcCacheOperator;
import com.bc.ui.MainActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;

public class CommonUtils {
	
	public static String ToString(Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof String) {
			return (String) object;
		} else if (object instanceof Double) {
			return ((Double)object).doubleValue()+"";
		}
		return object.toString();
	}
	
	public static double ToDouble(Object object) {
		if (object == null) {
			return 0;
		}
		
		if (object instanceof String) {
			String str = (String) object;
			if (str.startsWith("$")) {
				str = str.substring(1);
				Log.d(CommonUtils.class.getSimpleName(), "ToDobule str: " + str);
				return Double.valueOf(str) * Constants.RMB_TO_DOLLAR;
			} else {
				return Double.valueOf((String)object);
			}
		} 
		
		Double priceDouble = (Double)object;
		return priceDouble.doubleValue();
	}
	
	public static String DisplayFormatString(String type, String num, Context context) {
		if (TextUtils.isEmpty(num)) {
			return num;
		}
		if (type.trim().equals("MtGoxTicker") || type.equals("796futuresTicker") || type.equals("bitstampTicker")) {
			return context.getResources().getString(R.string.formate_money_dollor, num);
		} else {
			return context.getResources().getString(R.string.formate_money, num);
		}
	}
	
	/**
	 * if left < right  return -1 <br/>
	 * if left == right return 0; <br/>
	 * if left > right  return 1;
	 * @param left
	 * @param right
	 * @return
	 */
	public static double BigThan(Object left, Object right) {
		double leftValue = ToDouble(left);
		double rightValue = ToDouble(right);
		return leftValue - rightValue;
	}
	
	
	public static String formatNumber(String pattern, Object obj) {
		DecimalFormat df = new DecimalFormat(pattern);
		return df.format(obj);
	}
	
	/**
	 * 获得一行文字高度
	 * 
	 * @param fontSize
	 * @return
	 */
	public static int getFontHeight(float fontSize) {
		Paint paint = new Paint();
		paint.setTextSize(fontSize);
		FontMetrics fm = paint.getFontMetrics();
		return (int) Math.ceil(fm.descent - fm.top) + 2;
	}

	/**
	 * 获得一行文字宽度
	 * 
	 * @param fontSize
	 * @return
	 */
	public static int getFontWidth(float fontSize) {
		Paint paint = new Paint();
		paint.setTextSize(fontSize);

		return (int) paint.measureText("我", 0, 1);
	}

	public static int dip2px(Context context, float dip) {
		Resources r = context.getResources();
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
		return (int) px;
	}

	/**
	 * px转dip
	 * 
	 * @param context
	 * @param pxValue
	 * @return
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		
		return (int) (pxValue / scale + 0.5f);
	}
	
	
	
	//============Notification ===============
	public static void sendNotification(Context context, String title, Bundle bundle) {
		long[] pattern = {500,500,500,500,500,500,500,500,500};
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
		.setSmallIcon(R.drawable.icon)
		.setContentTitle(title)
		.setContentText(context.getString(R.string.notification_text))
		.setLights(Color.BLUE, 500, 500)
		.setVibrate(pattern)
		.setWhen(System.currentTimeMillis());
		 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        if(alarmSound == null){
            alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            if(alarmSound == null){
            	alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            }
        }
        builder.setSound(alarmSound);
		Intent intent = new Intent(context, MainActivity.class);
		intent.putExtras(bundle);
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		Notification notification = builder.build();
		notification.contentIntent = pendingIntent;
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		getNotificationManager(context).notify(11, notification);
	}
	
	public static void sendBackgroundNotifi(Context context, String btcType, String price) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
		.setSmallIcon(R.drawable.icon)
		.setContentTitle(context.getString(R.string.notifi_title, btcType))
		.setContentText(context.getString(R.string.notifi_content, btcType, price))
		.setTicker(context.getString(R.string.notifi_content, btcType, price))
		.setWhen(System.currentTimeMillis());
		Intent intent = new Intent(context, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		Notification notification = builder.build();
		notification.contentIntent = pendingIntent;
		notification.flags = Notification.FLAG_ONGOING_EVENT;
	
		getNotificationManager(context).notify(BtcCacheOperator.BACKGROUND_NOTIFI_ID, notification);
	}
	
	public static void sendNotification(Context context, String title, String msg) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
			.setSmallIcon(R.drawable.icon)
			.setContentTitle(title)
			.setContentText(msg)
			.setTicker(msg)
			.setWhen(System.currentTimeMillis());
		Intent intent = new Intent(context, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		Notification notifi = builder.build();
		notifi.contentIntent = pendingIntent;
		notifi.flags = Notification.FLAG_ONGOING_EVENT;
		getNotificationManager(context).notify(BtcCacheOperator.BACKGROUND_NOTIFI_ID, notifi);
	}
	
	public static NotificationManager manager = null;
	public static NotificationManager getNotificationManager(Context context) {
		if (manager == null) {
			manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		return manager;
	}
	
	public static void cancelNotification(Context context, int id) {
		getNotificationManager(context).cancel(id);
	}
}
